<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Options extends MY_Admin {
    function __construct()
    {
        parent :: __construct();
        $this->lang->load('options', 'chinese');
    }
    
    public function index()
    {
        $datas['opt'] = $this->Opts->get_options();
        
        $this->load->view('admins/Options/base', $datas);
    }
    
    public function edit()
    {
        $datas['opt'] = '';
        if($name = $this->uri->segment(4))
        {
            $datas['opt'] = $this->Opts->get_one(urldecode($this->uri->segment(4)));
        }
        $this->load->view('admins/Options/edit', $datas);
    }
    
    public function doEdit()
    {
        // Validation rules
        $validation = array(
            array(
                'field' => 'keyname',
                'label' => lang('keyname'),
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'valuename',
                'label' => lang('valuename'),
                'rules' => 'required|trim'
            )
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['name']			= $this->security->xss_clean($this->input->post('keyname'));
        $user_data['value'] 		= $this->security->xss_clean($this->input->post('valuename'));
        if ($this->form_validation->run())
        {
            if($this->Opts->set_options($user_data))
                $this->goback(1);
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
}